package com.me {
	import com.me.components.EventGrid;

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	import mx.core.FlexGlobals;
	import mx.core.mx_internal;
	import mx.events.MoveEvent;

	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 下午04:24:47
	 */
	public class GridLayer extends SpriteVisualElement {
		public var grids:Array = [];

		private var _gridSize:int = 32;

		private var _gridStatus:Array = [];

		private var _cols:int = 0;

		private var _rows:int = 0;

		public function GridLayer(w:int, h:int, gridSizeD:int = 32) {
			super();
			this.mouseChildren = this.mouseEnabled = true;
			width = w;
			height = h;
			_gridSize = gridSizeD;
			_rows = height / _gridSize;
			_cols = width / _gridSize;
			for (var row:int = 0; row < _rows; row++) {
				var detail:Array = [];
				for (var col:int = 0; col < _cols; col++) {
					detail.push(0);
				}
				_gridStatus.push(detail);
			}
			drawGrid();
//			addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, moveMoveHandler);
//			addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
		}

		private function mouseHandler(e:MouseEvent):void {
//			enableMove = e.type == MouseEvent.MOUSE_DOWN;
		}

		public function addEventGrid():void {
			var g:EventGrid = new EventGrid(grids.length);
			g.dragArea.addEventListener(MouseEvent.MOUSE_DOWN, onGridDown);
			grids.push(g);
			setPosition(g, new Point(Math.abs(parent.x) + 50, Math.abs(parent.y) + 50));
			addChild(g);
		}

		private var dragC:SpriteVisualElement;

		private function onGridDown(e:MouseEvent):void {
			dragC = e.currentTarget.parent as SpriteVisualElement;
			dragC.stage.focus = dragC;
			dragC.addEventListener(Event.ENTER_FRAME, startDragC);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopDragC);
		}

		private function setPosition(target:SpriteVisualElement, dp:Point):void {
			dp.x = int(dp.x / 80) * 80;
			dp.y = int(dp.y / 80) * 80;
			if (dp.x >= this.width) {
				dp.x = this.width - 80;
			} else if (dp.x <= 0) {
				dp.x = 0;
			}
			if (dp.y >= this.height) {
				dp.y = this.height - 80;
			} else if (dp.y <= 0) {
				dp.y = 0;
			}
			target.x = dp.x;
			target.y = dp.y;
		}

		private function startDragC(e:Event):void {
			var dp:Point = globalToLocal(new Point(stage.mouseX, stage.mouseY));
			setPosition(e.currentTarget as SpriteVisualElement, dp);
		}

		private function stopDragC(e:MouseEvent):void {
			if (dragC) {
				stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragC);
				dragC.removeEventListener(Event.ENTER_FRAME, startDragC);
				dragC = null;
			}
		}

		private function moveMoveHandler(e:MouseEvent):void {
			var clickcol:int = int(e.localX / _gridSize);
			var clickrow:int = int(e.localY / _gridSize);
			FlexGlobals.topLevelApplication.status = "X:" + e.localX + ".Y:" + e.localY + "/行:" + clickrow + ".列:" + clickcol;
//			if (enableMove) {
//				drawMask(e.localX, e.localY, FlexGlobals.topLevelApplication.walkable);
//			}
		}

		public function drawMask(lx:Number, ly:Number, walkable:Boolean = false):void {
			var clickcol:int = int(lx / _gridSize);
			var clickrow:int = int(ly / _gridSize);

			if (clickrow >= _gridStatus.length || clickcol >= _gridStatus[0].length) {
				return;
			}

			_gridStatus[clickrow][clickcol] = walkable ? 1 : 0;

			drawGrid();
		}

		private function drawGrid():void {
			graphics.clear();

			graphics.beginFill(0xFF0000, .0);
			graphics.drawRect(0, 0, width, height);
			graphics.endFill();

			graphics.beginFill(0xFF0000, 1.0);

			for (var w:int = 1; w <= _cols; w++) {
				graphics.moveTo(w * _gridSize, 0);
				graphics.lineStyle(1, 0xFF0000, 1);
				graphics.lineTo(w * _gridSize, height);
			}

			for (var h:int = 1; h <= _rows; h++) {
				graphics.moveTo(0, h * _gridSize);
				graphics.lineStyle(1, 0xFF0000, 1);
				graphics.lineTo(width, h * _gridSize);
			}

			graphics.endFill();

			graphics.beginFill(0xFF0000, 0.4);

			for (var row:int = 0; row < _gridStatus.length; row++) {
				for (var col:int = 0; col < _gridStatus[row].length; col++) {
					if (_gridStatus[row][col]) {
						graphics.drawRect(col * _gridSize, row * _gridSize, _gridSize, _gridSize);
					}
				}
			}

			graphics.endFill();
		}

		public function clear():void {
			graphics.clear();
		}

		public function get gridSize():int {
			return _gridSize;
		}

		public function get gridStatus():Array {
			return _gridStatus;
		}

		public function get cols():int {
			return _cols;
		}

		public function get rows():int {
			return _rows;
		}
	}
}
